# coding=utf-8
# @Time    : 2018/3/7 17:23
# @Author  : Jiangxu
# @File    : MySqlTest.py
# @Software: PyCharm
import MySQLdb
db = MySQLdb.connect("localhost", "root", "", "testdb")
cursor = db.cursor()
# db = MySQLdb.connect("localhost", "root", "", "testdb")
# cursor = db.cursor()
# cursor.execute("SELECT VERSION()")
# data = cursor.fetchone()
# print "Database version : %s " % data

class MySqldbExecute:
	def __init__(self,sql):
		self.sql = sql


	# SQL 插入语句or更新语句
	def ExecuteUpdateOrInsert(self,sql):
		try:
			# 执行sql语句
			cursor.execute(sql)
			# 提交到数据库执行
			db.commit()
		except:
			# Rollback in case there is any error
			db.rollback()

	# SQL 查询语句
	def ExecuteSql(self,sql):
		try:
			# 执行SQL语句
			cursor.execute(sql)
			# 获取所有记录列表
			results = cursor.fetchall()
			for row in results:
				fname = row[0]
				lname = row[1]
				age = row[2]
				sex = row[3]
				income = row[4]
				# 打印结果
				print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
					  (fname, lname, age, sex, income)
		except:
			print "Error: unable to fecth data"


# sql = "SELECT * FROM EMPLOYEE WHERE INCOME > '%d'" % (1000)
# Sql = MySqldbExecute(sql)
# Sql.ExecuteSql(sql)


